﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WPF_SFrost
{
    public partial class FormContactAdd : Form
    {
        public SqlConnection conn = new SqlConnection(SqlDbHelper.ConnectionString);
        public SqlCommand cmd;
        public SqlDataAdapter sda;
        public DataSet ds;
        public FormContactAdd()
        {
            InitializeComponent();
        }

        private void FormContactAdd_Load(object sender, EventArgs e)
        {
            FillGroup();
        }
        
        public void FillGroup()
        {
            //string sql = "select * from contactgroup order by id desc";
            cmd = new SqlCommand("GetAllContactGroup", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            conn.Open();
            sda = new SqlDataAdapter(cmd);
            ds = new DataSet();
            sda.Fill(ds);
            cboGroup.DisplayMember = "groupname";
            cboGroup.ValueMember = "id";
            cboGroup.DataSource = ds.Tables[0];
            conn.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            //MessageBox.Show(cboGroup.SelectedValue.ToString());
            string name = txtName.Text.Trim();
            string phone = txtPhone.Text.Trim();
            string email = txtEmail.Text.Trim();
            string qq = txtQQ.Text.Trim();
            string workAddress = txtWorkAddress.Text.Trim();
            string workPhone = txtWorkPhone.Text.Trim();
            string homeAddress = txtHomeAddress.Text.Trim();
            string homePhone = txtHomePhone.Text.Trim();
            string memo = txtMemo.Text.Trim();
            //获取用户选择的分组编号
            int groupId = Convert.ToInt32(cboGroup.SelectedValue);
            if (!Check(name, phone, email, qq, workPhone, homePhone))
                return;
            //string sql = string.Format("insert into contact values ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}',{9})",
            //name, phone, email, qq, workAddress, workPhone, homeAddress, homePhone, memo,groupId);
            //cmd = new SqlCommand(sql, conn);
            conn.Open();
            cmd = new SqlCommand("InsertContact",conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@name", name);
            cmd.Parameters.AddWithValue("@phone", phone);
            cmd.Parameters.AddWithValue("@email", email);
            cmd.Parameters.AddWithValue("@qq", qq);
            cmd.Parameters.AddWithValue("@officeaddress", workAddress);
            cmd.Parameters.AddWithValue("@officephone", workPhone);
            cmd.Parameters.AddWithValue("@homeaddress", homeAddress);
            cmd.Parameters.AddWithValue("@homephone", homePhone);
            cmd.Parameters.AddWithValue("@memo", memo);
            cmd.Parameters.AddWithValue("@groupid", groupId);
            int n = cmd.ExecuteNonQuery();
            if (n == 1)
            {
                MessageBox.Show("添加联系人成功。");
                this.Close();
            }
            else
            {
                MessageBox.Show("添加联系人失败。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        bool Check(string name,string phone, string email,string qq,string officephone, string homephone)
        {
            if (name == "")
            {
                MessageBox.Show("联系人姓名不能为空。");
                txtName.Focus();
                return false;
            }
            if (!Utility.CheckMobilePhone(phone))
            {
                MessageBox.Show("手机号码格式不正确。");
                txtPhone.Focus();
                return false;
            }
            if (!Utility.CheckMail(email))
            {
                MessageBox.Show("邮箱地址格式错误！");
                txtEmail.Focus();
                return false;
            }
            if (!Utility.CheckQQ(qq))
            {
                MessageBox.Show("QQ号码不正确！");
                txtQQ.Focus();
                return false;
            }
            if (!Utility.CheckPhone(officephone))
            {
                MessageBox.Show("办公电话不正确！");
                txtWorkPhone.Focus();
                return false;
            }
            if (!Utility.CheckPhone(homephone))
            {
                MessageBox.Show("家庭电话不正确。");
                txtHomePhone.Focus();
                return false;
            }
            return true;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
